<template>
  <div>
    <el-card style="margin: 20px 0px">
      <CategorySelect :show="scene !== 0" @getCategoryId="getCategoryId" />
    </el-card>
    <el-card>
      <div v-show="scene == 0">
        <el-button type="primary" icon="el-icon-plus" style="margin-bottom: 20px" :disabled="!category3Id" @click="addSpu">添加SPU</el-button>
        <el-table style="width: 100%" border :data="records">
          <el-table-column type="index" label="序号" width="70" align="center" />
          <el-table-column prop="spuName" label="SPU名称" width="width" />
          <el-table-column prop="description" label="SPU描述" width="width" />
          <el-table-column prop="prop" label="操作" width="240">
            <template slot-scope="{row}">
              <hint-button type="success" icon="el-icon-plus" size="mini" title="添加SKU" @click="addSku(row)" />
              <hint-button type="warning" icon="el-icon-edit" size="mini" title="修改SPU" @click="updateSpu(row)" />
              <hint-button type="info" icon="el-icon-info" size="mini" title="查看当前SPU全部SKU列表" @click="handler(row)" />
              <template>
                <el-popconfirm :title="`确定删除：${row.spuName}`" @onConfirm="deleteSpu(row)">
                  <hint-button slot="reference" type="danger" icon="el-icon-delete" size="mini" title="删除SPU" />
                </el-popconfirm>
              </template>
            </template>
          </el-table-column>
        </el-table>
        <el-pagination
          style="text-align: center; margin-top: 20px"
          :current-page="page"
          :total="total"
          :page-sze="limit"
          :page-sizes="[5, 15]"
          layout="prev, pager, next, jumper, ->, sizes, total"
          @current-change="getSpuList"
          @size-change="handleCurrentChange"
        />
      </div>
      <SpuForm v-show="scene==1" ref="spu" @changeScene="changeScene" />
      <SkuForm v-show="scene==2" ref="sku" @changeScenes="changeScenes" />
      <el-dialog :title="`${spuName}的SKU列表`" :visible.sync="dialogTableVisible" :before-close="close">
        <el-table v-loading="loading" :data="gridData">
          <el-table-column prop="skuName" label="名称" width="width" />
          <el-table-column prop="price" label="价格" width="width" />
          <el-table-column prop="weight" label="重量" width="width" />
          <el-table-column label="默认图片" width="width">
            <template slot-scope="{row}">
              <img :src="row.skuDefaultImg" style="width: 100px; height: 100px">
            </template>
          </el-table-column>
        </el-table>
      </el-dialog>
    </el-card>
  </div>
</template>

<script>
import SpuForm from './SpuForm'
import SkuForm from './SkuForm'
export default {
  name: 'Spu',
  components: {
    SpuForm,
    SkuForm
  },
  data() {
    return {
      category1Id: '',
      category2Id: '',
      category3Id: '',
      isShowTable: true,
      page: 0,
      limit: 5,
      records: [], // SPU列表数据
      total: 0,
      scene: 0,
      dialogTableVisible: false,
      spuName: '',
      gridData: [],
      loading: true
    }
  },
  methods: {
    // 获取SPU列表数据
    async getSpuList(pager = 1) {
      this.page = pager
      const { page, limit, category3Id } = this
      const result = await this.$API.spu.reqSpuList(page, limit, category3Id)
      if (result.code === 200) {
        this.records = result.data.records
        this.total = result.data.total
      }
    },
    getCategoryId({ categoryId, level }) {
      if (level === 1) {
        this.category1Id = categoryId
        this.category2Id = ''
        this.category3Id = ''
      } else if (level === 2) {
        this.category2Id = categoryId
        this.category3Id = ''
      } else {
        this.category3Id = categoryId
        this.getSpuList()
      }
    },
    handleCurrentChange(limit) {
      this.limit = limit
      this.getSpuList()
    },
    addSpu() {
      this.scene = 1
      this.$refs.spu.addSpuData(this.category3Id)
    },
    updateSpu(row) {
      this.scene = 1
      this.$refs.spu.initSpuData(row)
    },
    changeScene({ scene, flag }) {
      this.scene = scene
      if (flag === '修改') {
        this.getSpuList(this.page)
      } else {
        this.getSpuList()
      }
    },
    // 关闭添加SKU
    changeScenes(scene) {
      this.scene = scene
      this.getSpuList(this.page)
    },
    async deleteSpu(row) {
      const result = await this.$API.spu.reqDeleteSpu(row.id)
      if (result.code === 200) {
        this.$message({ type: 'success', message: '删除成功' })
      }
      this.getSpuList(this.records.length > 1 ? this.page : this.page - 1)
    },
    addSku(row) {
      this.scene = 2
      this.$refs.sku.getData(this.category1Id, this.category2Id, row)
    },
    // 查看SKU
    async handler(row) {
      this.dialogTableVisible = true
      this.spuName = row.spuName
      const result = await this.$API.spu.reqSkuList(row.id)
      if (result.code === 200) {
        this.gridData = result.data
        this.loading = false
      }
    },
    // 关闭sku列表框时清除数据
    close(done) {
      this.loading = true
      this.gridData = []
      done()
    }
  }
}
</script>

<style scoped lang="less"></style>
